/*
 * TimeBlock.java
 * 
 * Copyright (c) 2008, Marshall Scorcio, Ralph Holzmann, 
 *                     Stewart Sweet, Tyler Hoersch, Jeffery Van Handel
 * All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions are met:
 *     * Redistributions of source code must retain the above copyright
 *       notice, this list of conditions and the following disclaimer.
 *     * Redistributions in binary form must reproduce the above copyright
 *       notice, this list of conditions and the following disclaimer in the
 *       documentation and/or other materials provided with the distribution.
 *     * Neither the name of the University of Wisconsin Oshkosh nor the
 *       names of its contributors may be used to endorse or promote products
 *       derived from this software without specific prior written permission.
 *
 * THIS SOFTWARE IS PROVIDED BY COPYRIGHT HOLDERS AND CONTRIBUTORS ''AS IS'' AND 
 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
 * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE 
 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR 
 * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER 
 * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
 * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE 
 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 * 
 */

import java.text.DecimalFormat;

public class TimeBlock implements java.io.Serializable
{
    private int startHour;
    private int startMinute;
    private int endHour;
    private int endMinute;
   
    public TimeBlock(TimeBlock time)
    {
        this.startHour = time.getStartHour();
        this.startMinute = time.getStartMinute();
        this.endHour = time.getEndHour();
        this.endMinute = time.getEndMinute();
    }

    public TimeBlock(int startHour, int startMinute, int endHour, int endMinute)
    {
        this.startHour = startHour;
        this.startMinute = startMinute;
        this.endHour = endHour;
        this.endMinute = endMinute;
    }
   
    public TimeBlock(String startEnd)
    {
        String[] times = startEnd.split(" - ");
        String[] startTimeParts = times[0].split(" ");
        String[] startTime = startTimeParts[0].split(":");
        if (startTimeParts[1].equals("PM"))
        {
            startHour = 12;
        }
        else
        {
            startHour = 0;
        }
        if (Integer.parseInt(startTime[0].trim()) != 12)
        {    
            startHour += Integer.parseInt(startTime[0].trim()); 
        }
        startMinute = Integer.parseInt(startTime[1].trim());
        
        String[] endTimeParts = times[1].split(" ");
        String[] endTime = endTimeParts[0].split(":");
        if (endTimeParts[1].equals("PM"))
        {
            endHour = 12;
        }
        else
        {
            endHour = 0;
        }
        if (Integer.parseInt(endTime[0].trim()) != 12)
        {  
            endHour += Integer.parseInt(endTime[0].trim());
        }
        endMinute = Integer.parseInt(endTime[1].trim());
    }

    public int getStartHour()
    {
        return startHour;
    }
    
    public int getStartMinute()
    {
        return startMinute;
    }
    
    public int getEndHour()
    {
        return endHour;
    }
    
    public int getEndMinute()
    {
        return endMinute;
    }
    
    public void setStartTime(int hour, int minute)
    {
        startHour = hour;
        startMinute = minute;
    }
    
    public void setEndTime(int hour, int minute)
    {
        endHour = hour;
        endMinute = minute;
    }
    
    public boolean contains(TimeBlock timeBlock)
    {
        boolean contains = false;
        
        if (((this.getStartHour() < timeBlock.getStartHour()) ||
             ((this.getStartHour() == timeBlock.getStartHour()) &&
              (this.getStartMinute() < timeBlock.getStartMinute())))
            &&
            ((this.getEndHour() > timeBlock.getEndHour()) ||
             ((this.getEndHour() == timeBlock.getEndHour()) &&
              (this.getEndMinute() > timeBlock.getEndMinute()))))
        {
            contains = true;
        }
        
        return contains;
    }
    
    public boolean overlaps(TimeBlock timeBlock)
    {
        boolean overlaps = false;
        
        if (timeBlock.contains(this))
        {
            overlaps = true;
        }
        else if ((((this.getStartHour() < timeBlock.getStartHour()) ||
                   ((this.getStartHour() == timeBlock.getStartHour()) &&
                    (this.getStartMinute() < timeBlock.getStartMinute())))
                  &&
                  ((this.getEndHour() > timeBlock.getStartHour()) ||
                   ((this.getEndHour() == timeBlock.getStartHour()) &&
                    (this.getEndMinute() > timeBlock.getStartMinute()))))
                 ||
                 (((this.getStartHour() < timeBlock.getEndHour()) ||
                   ((this.getStartHour() == timeBlock.getEndHour()) &&
                    (this.getStartMinute() < timeBlock.getEndMinute())))
                  &&
                  ((this.getEndHour() > timeBlock.getEndHour()) ||
                   ((this.getEndHour() == timeBlock.getEndHour()) &&
                    (this.getEndMinute() > timeBlock.getEndMinute())))))
        {
            overlaps = true;
        }
        
        return overlaps;
    }
    
    @Override
    public boolean equals(Object obj)
    {
        boolean equal = false;
        
        if (this == obj)
        {
            equal = true;
        }
        else if (obj instanceof TimeBlock)
        {
            TimeBlock block = (TimeBlock) obj;
            if ((this.startHour == block.getStartHour()) && (this.startMinute == block.getStartMinute()) &&
                (this.endHour == block.getEndHour()) && (this.endMinute == block.getEndMinute()))
            {
               equal = true;
            }
        }
        
        return equal;
    }
    
    @Override
    public String toString()
    {
        DecimalFormat numberFormat = new DecimalFormat("00");
        StringBuilder formatted = new StringBuilder();
        if (startHour % 12 == 0)
        {
            formatted.append(12);
        }
        else
        {
            formatted.append(numberFormat.format(startHour % 12));
        }
        formatted.append(":" + numberFormat.format(startMinute));
        if (startHour >= 12)
        {
            formatted.append(" PM");
        }
        else
        {
            formatted.append(" AM");
        }
        formatted.append(" - ");
        if (endHour % 12 == 0)
        {
            formatted.append(12);
        }
        else
        {
            formatted.append(numberFormat.format(endHour % 12));
        }
        formatted.append(":" + numberFormat.format(endMinute));
        if (endHour >= 12)
        {
            formatted.append(" PM");
        }
        else
        {
            formatted.append(" AM");
        }
        
        return formatted.toString();
    }
}

